<?php
namespace Shoptaepc\Controller;
use Think\Controller;
// 本代码运行在TAE端
class IndexController extends Controller {
	public function  __construct (){
		parent::__construct();
		// $this->assign("appkey",C("FT_KEY"));
		if(C("TAE")){
			Vendor("TopClient");
		}else{
			Vendor("taobaosdk.top.TopClient");
		}
		Vendor("taobaosdk.top.TopClient");
		Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
		Vendor("taobaosdk.top.RequestCheckUtil");
		$this->sid = (int)I("get.shop_id");
		$this->module_width = (int)I("get.module_width");
		$this->seller_nick = I("get.seller_nick");
		$this->top_parameters = I("get.top_parameters");
		$this->top_sign =  I("get.top_sign");
		$this->top_appkey =  I("get.top_appkey")?I("get.top_appkey"):C("SM_KEY");
		$this->top_session =  I("get.top_session");
		if(!$this->sid){
			// 没有店铺信息不能访问
			$this->assign("msg","抱歉，本活动只能在店铺内访问");
			$this->display("Error:not_allow");
			die();
		}
		$this->assign("sid",$this->sid);
	}
	public function index(){
		if(C("TAE")){
			$fetchPostResult=$fetchService->fetchByGet(C("URL")."?m=shoptaepc&c=json&sid=".$this->sid);
			$data = json_decode($fetchPostResult,true);
		}else{
			$params = Array(
					"m"=>"shoptaepc",
					"c"=>"Json",
					"a"=>"index",
					"sid"=>$this->sid
				  );
			$data = json_decode($this->http(C("URL"),$params),true);
		}
		if($data["hasEvent"]=="false"){
			$this->assign("msg","当前时间没有活动！");
			$this->display("Error:not_allow");
			die();	
		}		
		if(!in_array($data["ctp"],C("SUPPORT_MOB"))){
			$this->assign("msg","抱歉，不支持当前的模板哦！");
			$this->display("Error:not_allow");
			die();	
		}
		foreach($data["winner"] as $j=>$win){
			$data["winner"][$j]["usernick"] = urldecode($win["usernick"]);
			$data["winner"][$j]["name"] = urldecode($win["name"]);
		}
		foreach($data["gifts"] as $k=>$gift){
			$data["gifts"][$k]["name"] = urldecode($data["gifts"][$k]["name"]);
			$data["gifts"][$k]["type"] = urldecode($data["gifts"][$k]["type"]);
			$data["gifts"][$k]["level_name"] = urldecode($data["gifts"][$k]["level_name"]);
		}
		$this->assign("dicescfg",$data["dicescfg"]);
		$this->assign("front_cfg",$data["front_cfg"]);
		$this->assign("ddcj",$data["cfg"]["orderset"]);
		$this->assign("errmsg",$data["errmsg"]);
		$this->assign("cfgok",$data["cfgok"]);
		$this->assign("contdown",$data["contdown"]);
		// $this->assign("contdown",0);
		$this->assign("cfg",$data["cfg"]);
		$this->assign("ctp",$data["cfg"]["curtool"]);
		$this->assign("ctptpl","./".$data["cfg"]["curtool"].".inc.html");
		$this->assign("lotts",$data["cfg"]["lotts"]);
		$this->assign("wins",$data["cfg"]["wins"]);
		$this->assign("gifts",$data["gifts"]);
		// if(C("TAE")){
			// $this->assign("winner",urldecode_utf8($data["winner"]));
		// }else{
			$this->assign("winner",$data["winner"]);
		// }
		$this->display();
	}
	
	public function http($url, $params, $method = 'GET', $header = array(), $multi = false){
		$opts = array(
				CURLOPT_TIMEOUT        => 30,
				CURLOPT_RETURNTRANSFER => 1,
				CURLOPT_SSL_VERIFYPEER => false,
				CURLOPT_SSL_VERIFYHOST => false,
				CURLOPT_HTTPHEADER     => $header
		);
		/* 根据请求类型设置特定参数 */
		switch(strtoupper($method)){
			case 'GET':
				$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
				break;
			case 'POST':
				//判断是否传输文件
				$params = $multi ? $params : http_build_query($params);
				$opts[CURLOPT_URL] = $url;
				$opts[CURLOPT_POST] = 1;
				$opts[CURLOPT_POSTFIELDS] = $params;
				break;
			default:
				throw new \Exception('不支持的请求方式！');
		}
		/* 初始化并执行curl请求 */
		$ch = curl_init();
		curl_setopt_array($ch, $opts);
		$data  = curl_exec($ch);
		$error = curl_error($ch);
		curl_close($ch);
		if($error) throw new \Exception('请求发生错误：' . $error);
		return  $data;
	}
}